package springmvc.common;

import java.io.IOException;
import java.text.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import BaseDBEntity.BaseService;
import io.swagger.annotations.ApiOperation;

/**
 * 功能说明：springmvc的基类，处理异常，公共方法
 * 
 * @author ducc
 * 
 * @updated
 * @param <T>
 */

public class SpringBaseController<T> extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected BaseService<T> baseService;

	@RequestMapping(value = "selectAll", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	@ApiOperation(value = "获取所有数据", notes = "获取所有数据", httpMethod = "GET")
	public String selectAll() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
			IOException, ParseException {

		return getResult(baseService.selectAll(), "获取所有数据", 1);

	}

	@RequestMapping(value = "insert", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	@ApiOperation(value = "插入数据", notes = "插入数据", httpMethod = "POST")
	public String insert(T object) throws InstantiationException, IllegalAccessException, IllegalArgumentException,
			IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.insert(object), "插入数据", 1);

	}

	@RequestMapping(value = "queryList", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryList(T object) throws InstantiationException, IllegalAccessException, IllegalArgumentException,
			IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.queryList(object), "根据条件获取数据", 1);

	}

	@RequestMapping(value = "deleteList", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteList(T object) throws InstantiationException, IllegalAccessException, IllegalArgumentException,
			IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.deleteList(object), "根据条件删除数据", 1);

	}

	@RequestMapping(value = "deleteByPrimaryKeys", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteByPrimaryKeys(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.deleteByPrimaryKeys(object), "根据主键删除数据", 1);

	}

	@RequestMapping(value = "selectByPrimaryKeysToOne", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String selectByPrimaryKeysToOne(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.selectByPrimaryKeysToOne(object), "根据主键获取唯一数据", 1);

	}

	@RequestMapping(value = "selectByPrimaryKeysToList", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String selectByPrimaryKeysToList(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.selectByPrimaryKeysToList(object), "根据主键获取一组数据", 1);

	}

	@RequestMapping(value = "getCount", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getCount() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
			IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.getCount(), "获取数据个数", 1);

	}

	@RequestMapping(value = "insertSelective", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String insertSelective(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.insertSelective(object), "插入数据,忽略为空的数据", 1);

	}

	@RequestMapping(value = "updateByPrimaryKeys", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String updateByPrimaryKeys(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.updateByPrimaryKeys(object), "根据主键更新数据", 1);

	}

	@RequestMapping(value = "updateByPrimaryKeysSelective", method = RequestMethod.GET, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String updateByPrimaryKeysSelective(T object) throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, IOException, ParseException {
		/* object.getClass().getName() */

		return getResult(baseService.updateByPrimaryKeysSelective(object), "根据主键更新数据,忽略为空的数据", 1);

	}
}
